package course.filter;

import course.pojo.entity.Teacher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author HLF
 * @date 2022/7/5 15:12
 */
@WebFilter(filterName = "TeacherLoginFilter",
        value = {
        "/teacher/teacherOwnController/*",
        "/tea_center_update.jsp",
        "/tea_ownstu.jsp",
        "/teacher_center.jsp",
        "/teasub_manage.jsp",
})
public class TeacherLoginFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        HttpSession session = req.getSession();
        Teacher teacher = (Teacher) session.getAttribute("tteacher");
        if (teacher == null) {
            req.setAttribute("roleMsg", "未登录或教师权限不足，无法访问！");
            req.getRequestDispatcher("/422.jsp").forward(req, resp);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
